package com.yaozheng.vocationaltraining.fragment.exam;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.IMasterView;
import com.yaozheng.vocationaltraining.service.MasterService;
import com.yaozheng.vocationaltraining.service.impl.MasterServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.ExamProgressStatusView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 大师之路
 */
@EFragment(R.layout.activity_master)
public class MasterFragment extends BaseFragment implements IMasterView {

    @Bean(MasterServiceImpl.class)
    MasterService masterService;

    JSONArray mastersArray;
    @ViewById
    TextView headTitleText;

    @ViewById
    ExamProgressStatusView maintenanceMasterProgressStatusView1, maintenanceMasterProgressStatusView2,
            serviceMasterProgressStatusView3, salesMasterProgressStatusView4, goodsMasterProgressStatusView5,
            brandMasterProgressStatusView6, goodsMasterProgressStatusView7;

    //    ExamProgressStatusView[] maintenanceMasterProgressStatusViews;
    Map<Integer, ExamProgressStatusView> maintenanceMasterProgressStatusViews;

    @AfterViews
    public void initView() {
        headTitleText.setText("大师之路");
        maintenanceMasterProgressStatusViews = new HashMap<Integer, ExamProgressStatusView>();
        maintenanceMasterProgressStatusViews.put(6, brandMasterProgressStatusView6);
        maintenanceMasterProgressStatusViews.put(5, goodsMasterProgressStatusView5);
        maintenanceMasterProgressStatusViews.put(7, goodsMasterProgressStatusView7);
        maintenanceMasterProgressStatusViews.put(4, salesMasterProgressStatusView4);
        maintenanceMasterProgressStatusViews.put(3, serviceMasterProgressStatusView3);
        maintenanceMasterProgressStatusViews.put(2, maintenanceMasterProgressStatusView2);
        maintenanceMasterProgressStatusViews.put(1, maintenanceMasterProgressStatusView1);
        masterService.init(this);
        loadData();


//        maintenanceMasterProgressStatusViews = new ExamProgressStatusView[]{maintenanceMasterProgressStatusView1,
//                maintenanceMasterProgressStatusView2, serviceMasterProgressStatusView3, salesMasterProgressStatusView4,
//                goodsMasterProgressStatusView5, brandMasterProgressStatusView6,goodsMasterProgressStatusView7};
    }

    public void loadData() {
        masterService.masterProgress();
    }


    @UiThread
    @Override
    public void masterProgressSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        this.mastersArray = TypeUtils.getJsonArray(jsonObject, "data", "masters");
        updateData();
    }

    /**
     * 刷新数据
     */
    public void updateData() {
//        masters
        if (mastersArray == null) {

//            for (int i = 0; i < maintenanceMasterProgressStatusViews.length; i++) {
//                ExamProgressStatusView maintenanceMasterProgressStatusView = maintenanceMasterProgressStatusViews[i];
//                maintenanceMasterProgressStatusView.setStatus(-1);
//            }
            Iterator<Integer> iterator = maintenanceMasterProgressStatusViews.keySet().iterator();
            while (iterator.hasNext()) {
                Integer key = iterator.next();
                ExamProgressStatusView maintenanceMasterProgressStatusView = maintenanceMasterProgressStatusViews.get(key);
                maintenanceMasterProgressStatusView.setStatus(-1);
            }


        } else {
            for (int i = 0; i < TypeUtils.getJsonArraySize(mastersArray); i++) {
                JSONObject masterJsonObject = TypeUtils.getJsonObject(mastersArray, i);
                int id = TypeUtils.getJsonInteger(masterJsonObject, "id");
                int step = TypeUtils.getJsonInteger(masterJsonObject, "step");
                ExamProgressStatusView maintenanceMasterProgressStatusView = maintenanceMasterProgressStatusViews.get(id);
                if (maintenanceMasterProgressStatusView != null) {
                    if (step == 0) {
                        maintenanceMasterProgressStatusView.setStatus(-1);
                    } else if (step >= 1 && step < 9) {
                        maintenanceMasterProgressStatusView.setStatus(-2);
                    } else if (step >= 9) {
                        maintenanceMasterProgressStatusView.setStatus(-3);
                    }
                }
            }
        }

    }

    @UiThread
    @Override
    public void masterProgressError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @Override
    public void onDestroyView() {
        mastersArray = null;
        super.onDestroyView();
    }
}
